
// socketHandler.js
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()

module.exports = function handleSocketConnection(socket, acceptIdToSocketIdMap) {
    // 这里是你的连接处理逻辑
    // 例如：
    // 发送消息
    // 接收消息
    socket.on('sendMsg', (data) => {
        const { acceptId, sendId, text } = data
        socket.to(acceptIdToSocketIdMap[acceptId]).emit('pushMsg', {id:sendId, content: text, acceptId})
    });

    socket.on('logout', (data) => {
        // 修改最后在线时间
        const { userId } = data
        prisma.user.update({
            where: {
                id: userId
            },
            data: {
                lastAt: new Date()
            }
        })
        .then(result => {
            console.log(result);
        })
        .catch(err => 
            console.log(err.message)
        )
        .finally(async() => prisma.$disconnect())
    })

};
